home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Graphics / GifScan 1.6 / Sources / GSTEStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  13.2 KB  |  567 lines  |  [TEXT/MMCC]

  1. #include "GifScan.h"
  2. #include "GSDrag.h"
  3. #include "Prefs.h"
  4.  
  5. extern     thePrefsHandle    gPrefs;
  6. extern    Boolean            gHasDrag;
  7.  
  8. // Most of this stuff from Apple's TE Sample 
  9. pascal void PascalClikLoop(void)
  10. {
  11.     WindowRef    window;
  12.     RgnHandle    region;
  13.     
  14.     window = FrontWindow();
  15.     region = NewRgn();
  16.     GetClip(region);
  17.     ClipRect(&window->portRect);
  18.     AdjustScrollValues(window, true);
  19.     SetClip(region);
  20.     DisposeRgn(region);
  21. }
  22.  
  23. pascal ProcPtr GetOldClikLoop(void)
  24. {
  25.     return ((DocumentPeek)FrontWindow())->docClik;
  26. }
  27.  
  28. asm pascal void AsmClikLoop(void)
  29. {
  30.     MOVEM.L        D1-D2/A1,-(SP)        // D0 and A0 need not be saved
  31.     CLR.L        -(SP)                // make space for procedure pointer
  32.     JSR            GetOldClikLoop        // get the old clikLoop
  33.     MOVEA.L        (SP)+,A0            // into A0
  34.     MOVEM.L        (SP)+,D1-D2/A1        // restore the world as it was
  35.     
  36.     JSR            (A0)                // and execute old clikLoop
  37.  
  38.     MOVEM.L        D1-D2/A1,-(SP)        // D0 and A0 need not be saved
  39.     JSR            PascalClikLoop        // do our clikLoop
  40.     MOVEM.L        (SP)+,D1-D2/A1        // restore the world as it was
  41.     MOVEQ        #1,D0                // clear the zero flag so TextEdit keeps going
  42.     RTS
  43. }
  44.  
  45. void GetGlobalMouse(Point *mouse)
  46. {
  47.     EventRecord    event;
  48.     
  49.     OSEventAvail(kNoEvents, &event);
  50.     *mouse = event.where;
  51. }
  52.  
  53. void AdjustHV(Boolean isVert, ControlHandle control,
  54.               TEHandle docTE, Boolean canRedraw)
  55. {
  56.     short        value, lines, max;
  57.     short        oldValue, oldMax;
  58.     TEPtr        te;
  59.     
  60.     oldValue = GetCtlValue(control);
  61.     oldMax = GetCtlMax(control);
  62.     te = *docTE;
  63.     if ( isVert ) 
  64.     {
  65.         lines = te->nLines;
  66.  
  67.         if ( *(*te->hText + te->teLength - 1) == kCrChar )
  68.             lines += 1;
  69.         max = lines - ((te->viewRect.bottom - te->viewRect.top) /
  70.                 te->lineHeight);
  71.     } 
  72.     else
  73.         max = kMaxDocWidth - (te->viewRect.right - te->viewRect.left);
  74.     
  75.     if ( max < 0 ) max = 0;
  76.     SetCtlMax(control, max);
  77.     
  78.     te = *docTE;
  79.     if ( isVert )
  80.         value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
  81.     else
  82.         value = te->viewRect.left - te->destRect.left;
  83.     
  84.     if ( value < 0 ) value = 0;
  85.     else if ( value >  max ) value = max;
  86.     
  87.     SetCtlValue(control, value);
  88.  
  89.     if ( canRedraw || (max != oldMax) || (value != oldValue) )
  90.         ShowControl(control);
  91. }
  92.  
  93. void AdjustScrollValues(WindowRef window, Boolean canRedraw)
  94. {
  95.     DocumentPeek doc;
  96.     
  97.     doc = (DocumentPeek)window;
  98.     AdjustHV(true, doc->docVScroll, doc->docTE, canRedraw);
  99.     AdjustHV(false, doc->docHScroll, doc->docTE, canRedraw);
  100. }
  101.  
  102. Boolean IsDialogWindow(WindowRef window)
  103. {
  104.     if ( window == nil )
  105.         return(false);
  106.     else
  107.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  108. }
  109.  
  110. Boolean IsAppWindow(WindowRef window)
  111. {
  112.     short        windowKind;
  113.  
  114.     if ( window == nil )
  115.         return false;
  116.     else 
  117.     {
  118.         windowKind = ((WindowPeek) window)->windowKind;
  119.         return (windowKind == userKind );
  120.     }
  121. }
  122.  
  123. Boolean IsDAWindow(WindowRef window)
  124. {
  125.     if ( window == nil )
  126.         return false;
  127.     else
  128.         return ((WindowPeek) window)->windowKind < 0;
  129. }
  130.  
  131. Boolean DoCloseWindow(WindowRef    window)
  132. {
  133.     TEHandle    te;
  134.  
  135.     if ( IsDAWindow(window) )
  136.         CloseDeskAcc(((WindowPeek) window)->windowKind);
  137.     else if ( IsAppWindow(window) ) 
  138.     {
  139.         te = ((DocumentPeek) window)->docTE;
  140.         if ( te != nil )
  141.         {
  142.             TEDispose(te);
  143.         }
  144.         CloseWindow(window);
  145.         DisposePtr((Ptr) window);
  146.     }
  147.     return true;
  148. }
  149.  
  150. void AdjustViewRect(TEHandle docTE)
  151. {
  152.     TEPtr        te;
  153.     
  154.     te = *docTE;
  155.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  156.                             * te->lineHeight) + te->viewRect.top;
  157. }
  158.  
  159. void GetTERect(WindowRef window, Rect *teRect)
  160. {
  161.     *teRect = window->portRect;
  162.     InsetRect(teRect, kTextMargin, kTextMargin);
  163.     teRect->bottom = teRect->bottom - 15;
  164.     teRect->right = teRect->right - 15;
  165. }
  166.  
  167. void GetLocalUpdateRgn(WindowRef window, RgnHandle localRgn)
  168. {
  169.     CopyRgn(((WindowPeek) window)->updateRgn, localRgn);
  170.     OffsetRgn(localRgn, window->portBits.bounds.left, window->portBits.bounds.top);
  171. }
  172.  
  173. void DrawWindow(WindowRef window)
  174. {
  175.     SetPort(window);
  176.     EraseRect(&window->portRect);
  177.     DrawControls(window);
  178.     DrawGrowIcon(window);
  179.     TEUpdate(&window->portRect, ((DocumentPeek) window)->docTE);
  180. }
  181.  
  182. void AdjustScrollSizes(WindowRef window)
  183. {
  184.     Rect        teRect;
  185.     DocumentPeek doc;
  186.     
  187.     doc = (DocumentPeek) window;
  188.     GetTERect(window, &teRect);
  189.     (*doc->docTE)->viewRect = teRect;
  190.     AdjustViewRect(doc->docTE);
  191.     if ( gHasDrag ) TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
  192.         
  193.     MoveControl(doc->docVScroll, window->portRect.right - kScrollbarAdjust, -1);
  194.     SizeControl(doc->docVScroll, kScrollbarWidth, (window->portRect.bottom - 
  195.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  196.     MoveControl(doc->docHScroll, -1, window->portRect.bottom - kScrollbarAdjust);
  197.     SizeControl(doc->docHScroll, (window->portRect.right - 
  198.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  199.                 kScrollbarWidth);
  200. }
  201.  
  202. void AdjustScrollbars(WindowRef window, Boolean needsResize)
  203. {
  204.     DocumentPeek doc;
  205.     
  206.     doc = (DocumentPeek) window;
  207.  
  208.     (*doc->docVScroll)->contrlVis = kControlInvisible;
  209.     (*doc->docHScroll)->contrlVis = kControlInvisible;
  210.     if ( needsResize )
  211.         AdjustScrollSizes(window);
  212.     AdjustScrollValues(window, needsResize);
  213.  
  214.     (*doc->docVScroll)->contrlVis = kControlVisible;
  215.     (*doc->docHScroll)->contrlVis = kControlVisible;
  216. }
  217.  
  218. void AdjustTE(WindowRef window)
  219. {
  220.     TEPtr        te;
  221.     
  222.     te = *((DocumentPeek)window)->docTE;
  223.     TEScroll((te->viewRect.left - te->destRect.left) -
  224.             GetCtlValue(((DocumentPeek)window)->docHScroll),
  225.             (te->viewRect.top - te->destRect.top) -
  226.                 (GetCtlValue(((DocumentPeek)window)->docVScroll) *
  227.                 te->lineHeight),
  228.             ((DocumentPeek)window)->docTE);
  229. }
  230.  
  231. void ResizeWindow(WindowRef    window)
  232. {
  233.     AdjustScrollbars(window, true);
  234.     AdjustTE(window);
  235.     InvalRect(&window->portRect);
  236. }
  237.  
  238. void DoGrowWindow(WindowRef window, EventRecord    *event)
  239. {
  240.     long        growResult;
  241.     Rect        tempRect;
  242.     RgnHandle    tempRgn;
  243.     DocumentPeek doc;
  244.     
  245.     tempRect = qd.screenBits.bounds;
  246.     tempRect.left = kMinDocDim;
  247.     tempRect.top = kMinDocDim;
  248.     growResult = GrowWindow(window, event->where, &tempRect);
  249.  
  250.     if ( growResult != 0 ) 
  251.     {
  252.         doc = (DocumentPeek) window;
  253.         tempRect = (*doc->docTE)->viewRect;
  254.         tempRgn = NewRgn();
  255.         GetLocalUpdateRgn(window, tempRgn);
  256.         SizeWindow(window, LoWrd(growResult), HiWrd(growResult), true);
  257.         ResizeWindow(window);
  258.  
  259.         SectRect(&tempRect, &(*doc->docTE)->viewRect, &tempRect);
  260.         ValidRect(&tempRect);
  261.         InvalRgn(tempRgn);
  262.         DisposeRgn(tempRgn);
  263.     }
  264. }
  265.  
  266. void DoZoomWindow(WindowRef window, short part)
  267. {
  268.     EraseRect(&window->portRect);
  269.     ZoomWindow(window, part, window == FrontWindow());
  270.     ResizeWindow(window);
  271. }
  272.  
  273. static void CheckForWindowFit(void)
  274. {
  275.     thePrefsHandle    resHandle;
  276.     
  277.     // If window is (partly) outside the screen, reset to default size and position in
  278.     // order to avoid problems when moving to smaller screens (window invisible)
  279.     if ( !RectInRect(&(*gPrefs)->winRect, &qd.screenBits.bounds) )
  280.     {
  281.         resHandle = (thePrefsHandle)Get1Resource(kPrefResType, prefsResNum);
  282.         (*gPrefs)->winRect = (*resHandle)->winRect;
  283.     }
  284. }
  285.  
  286. Boolean DoNew(void)
  287. {
  288.     Boolean            good;
  289.     Ptr                storage;
  290.     Rect            destRect, viewRect;
  291.     DocumentPeek    doc;
  292.     Rect            receiveRect;
  293.     RgnHandle        dragReceiveRgn;
  294.     DragInfoHandle    dragInfo;
  295.     OSErr            err;
  296.     TextStyle        style;
  297.     short            lineHeight, ascent;
  298.     Boolean            succes = true;
  299.     
  300.     storage = NewPtr(sizeof(DocumentRecord));
  301.     if ( storage != nil ) 
  302.     {
  303.         gOutWindow = GetNewWindow(theWindResID, storage, (WindowRef)-1L);
  304.         if ( gOutWindow != nil ) 
  305.         {
  306.             good = false;
  307.             SetPort(gOutWindow);
  308.             doc =  (DocumentPeek) gOutWindow;
  309.             GetTERect(gOutWindow, &viewRect);
  310.             destRect = viewRect;
  311.             destRect.right = destRect.left + kMaxDocWidth;
  312.             doc->docHiliteRgn = NewRgn();
  313.             doc->docTE = TENew(&destRect, &viewRect);
  314.             good = doc->docTE != nil;
  315.             if ( good )
  316.             {
  317.                 (*(doc->docTE))->txFont = (*gPrefs)->fontNum;
  318.                 (*(doc->docTE))->txSize = (*gPrefs)->fontSize;
  319.                 TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
  320.                 (*(doc->docTE))->fontAscent = ascent;
  321.                 (*(doc->docTE))->lineHeight = lineHeight;
  322.                 AdjustViewRect(doc->docTE);
  323.                 TEAutoView(true, doc->docTE);
  324.                 doc->docClik = (ProcPtr) (*doc->docTE)->clickLoop;
  325.                 (*doc->docTE)->clickLoop = (TEClickLoopUPP) AsmClikLoop;
  326.             }
  327.             else
  328.                 return (!succes);
  329.             
  330.             if ( good ) 
  331.             {
  332.                 doc->docVScroll = GetNewControl(rVScroll, gOutWindow);
  333.                 good = (doc->docVScroll != nil);
  334.             }
  335.             else
  336.                 return (!succes);
  337.                 
  338.             if ( good) 
  339.             {
  340.                 doc->docHScroll = GetNewControl(rHScroll, gOutWindow);
  341.                 good = (doc->docHScroll != nil);
  342.             }
  343.             else
  344.                 return (!succes);
  345.             
  346.             if ( good ) 
  347.             {
  348.                 CheckForWindowFit();    // Check if window will be on the screen
  349.                 SizeWindow(gOutWindow, 
  350.                     (*gPrefs)->winRect.right - (*gPrefs)->winRect.left,
  351.                     (*gPrefs)->winRect.bottom - (*gPrefs)->winRect.top, true);
  352.                 MoveWindow(gOutWindow, (*gPrefs)->winRect.left, (*gPrefs)->winRect.top, true);
  353.                 AdjustScrollbars(gOutWindow, true);
  354.                 ShowWindow(gOutWindow);
  355.             } 
  356.             else 
  357.             {
  358.                 DoCloseWindow(gOutWindow);
  359.                 return(!succes);
  360.             }
  361.             
  362.             if ( gHasDrag )
  363.             {
  364.                 dragInfo = (DragInfoHandle)NewHandle(sizeof(DragInfoRecord));
  365.                 dragReceiveRgn = NewRgn();
  366.                 receiveRect = gOutWindow->portRect;
  367.                 receiveRect.bottom -= kScrollbarAdjust;
  368.                 receiveRect.right -= kScrollbarAdjust;
  369.                 RectRgn(dragReceiveRgn, &receiveRect);
  370.                 
  371.                 (*dragInfo)->window = gOutWindow;
  372.                 (*dragInfo)->flavor = flavorTypeHFS;
  373.                 (*dragInfo)->receiveRgn = dragReceiveRgn;
  374.                 (*dragInfo)->receiveFunc = ReceiveHFSDrag;
  375.                 
  376.                 SetWRefCon(gOutWindow, (long)dragInfo);
  377.                 
  378.                 err = InitDragInfo(dragInfo);
  379.                 if (  err != noErr ) SysBeep(20);
  380.             }
  381.             return (succes);
  382.         }
  383.         else
  384.         {
  385.             DisposePtr(storage);
  386.             return (!succes);
  387.         }
  388.     }
  389.     else
  390.         return(!succes);
  391. }
  392.  
  393. void CommonAction(ControlRef control, short *amount)
  394. {
  395.     short        value, max;
  396.     
  397.     value = GetCtlValue(control);
  398.     max = GetCtlMax(control);
  399.     *amount = value - *amount;
  400.     if ( *amount < 0 )
  401.         *amount = 0;
  402.     else if ( *amount > max )
  403.         *amount = max;
  404.     SetCtlValue(control, *amount);
  405.     *amount = value - *amount;
  406. }
  407.  
  408. pascal void VActionProc(ControlRef control, short part)
  409. {
  410.     short        amount;
  411.     WindowRef    window;
  412.     TEPtr        te;
  413.     
  414.     if ( part != 0 ) 
  415.     {
  416.         window = (*control)->contrlOwner;
  417.         te = *((DocumentPeek) window)->docTE;
  418.         switch ( part ) 
  419.         {
  420.             case inUpButton:
  421.             case inDownButton:
  422.                 amount = 1;
  423.                 break;
  424.             case inPageUp:
  425.             case inPageDown:
  426.                 amount = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
  427.                 break;
  428.         }
  429.         if ( (part == inDownButton) || (part == inPageDown) )
  430.             amount = -amount;
  431.         CommonAction(control, &amount);
  432.         if ( amount != 0 )
  433.             TEScroll(0, amount * te->lineHeight, ((DocumentPeek) window)->docTE);
  434.     }
  435.  
  436. pascal void HActionProc(ControlRef control, short part)
  437. {
  438.     short        amount;
  439.     WindowRef    window;
  440.     TEPtr        te;
  441.     
  442.     if ( part != 0 ) 
  443.     {
  444.         window = (*control)->contrlOwner;
  445.         te = *((DocumentPeek) window)->docTE;
  446.         switch ( part ) {
  447.             case inUpButton:
  448.             case inDownButton:
  449.                 amount = kButtonScroll;
  450.                 break;
  451.             case inPageUp:
  452.             case inPageDown:
  453.                 amount = te->viewRect.right - te->viewRect.left;
  454.                 break;
  455.         }
  456.         if ( (part == inDownButton) || (part == inPageDown) )
  457.             amount = -amount;
  458.         CommonAction(control, &amount);
  459.         if ( amount != 0 )
  460.             TEScroll(amount, 0, ((DocumentPeek) window)->docTE);
  461.     }
  462. }
  463.  
  464. void DoContentClick(WindowRef window, EventRecord *event)
  465. {
  466.     Point            mouse;
  467.     ControlHandle     control;
  468.     short            part, value;
  469.     Boolean            shiftDown;
  470.     DocumentPeek     doc;
  471.     Rect            teRect;
  472.  
  473.     if ( IsAppWindow(window) ) 
  474.     {
  475.         SetPort(window);
  476.         mouse = event->where;
  477.         GlobalToLocal(&mouse);
  478.         doc = (DocumentPeek) window;
  479.         GetTERect(window, &teRect);
  480.         if ( PtInRect(mouse, &teRect) ) 
  481.         {
  482.             shiftDown = (event->modifiers & shiftKey) != 0;
  483.             TEClick(mouse, shiftDown, doc->docTE);
  484.         } 
  485.         else 
  486.         {
  487.             part = FindControl(mouse, window, &control);
  488.             switch ( part ) 
  489.             {
  490.                 case 0:
  491.                     break;
  492.                 
  493.                 case inThumb:
  494.                     value = GetCtlValue(control);
  495.                     part = TrackControl(control, mouse, nil);
  496.                     if ( part != 0 ) 
  497.                     {
  498.                         value -= GetCtlValue(control);
  499.                         if ( value != 0 )
  500.                             if ( control == doc->docVScroll )
  501.                                 TEScroll(0, value * (*doc->docTE)->lineHeight, doc->docTE);
  502.                             else
  503.                                 TEScroll(value, 0, doc->docTE);
  504.                     }
  505.                     break;
  506.                 
  507.                 default:
  508.                     if ( control == doc->docVScroll )
  509.                         value = TrackControl(control, mouse, (ControlActionUPP) VActionProc);
  510.                     else
  511.                         value = TrackControl(control, mouse, (ControlActionUPP) HActionProc);
  512.                     break;
  513.             }
  514.         }
  515.     }
  516. }
  517.  
  518. void UpdateWindowFontSize(short theSize)
  519. {
  520.     TextStyle        style;
  521.     short            lineHeight, ascent;
  522.     DocumentPeek    doc;
  523.     Rect            aRect;
  524.     
  525.     if ( gOutWindow != nil )
  526.     {
  527.         doc =  (DocumentPeek)gOutWindow;
  528.         (*(doc->docTE))->txSize = theSize;
  529.         TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
  530.         (*(doc->docTE))->fontAscent = ascent;
  531.         (*(doc->docTE))->lineHeight = lineHeight;
  532.         aRect = (*(doc->docTE))->viewRect;
  533.         EraseRect(&aRect);
  534.         InvalRect(&aRect);
  535.         TECalText(doc->docTE);
  536.         AdjustViewRect(doc->docTE);
  537.         AdjustTE(gOutWindow);
  538.         AdjustScrollbars(gOutWindow, true);
  539.         TESetSelect(kMaxTELength, kMaxTELength, doc->docTE);    // end of window
  540.     }
  541. }
  542.  
  543. void UpdateWindowFont(short fontNum)
  544. {
  545.     TextStyle        style;
  546.     short            lineHeight, ascent;
  547.     DocumentPeek    doc;
  548.     Rect            aRect;
  549.     
  550.     if ( gOutWindow != nil )
  551.     {
  552.         doc =  (DocumentPeek)gOutWindow;
  553.         (*(doc->docTE))->txFont= fontNum;
  554.         TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
  555.         (*(doc->docTE))->fontAscent = ascent;
  556.         (*(doc->docTE))->lineHeight = lineHeight;
  557.         aRect = (*(doc->docTE))->viewRect;
  558.         EraseRect(&aRect);
  559.         InvalRect(&aRect);
  560.         TECalText(doc->docTE);
  561.         AdjustViewRect(doc->docTE);
  562.         AdjustTE(gOutWindow);
  563.         AdjustScrollbars(gOutWindow, true);
  564.         TESetSelect(kMaxTELength, kMaxTELength, doc->docTE);
  565.     }
  566. }